// import { genMd5 } from "./encrypt";
import { isString, isBoolean, isObject, isType } from "@/utils/typeInspector";


function combine(prefix: string, key: string) {
  return prefix ? `${prefix}_${key}`: key;
}

function genStorage(storage: any, prefix: string, encrypts: Array<any> = []) {
 
  let obj: any = {
    set: (key: string, value: any) => {
      storage.setItem(combine(prefix, key), JSON.stringify(value));
    },
    get: (key: string) => {
      const value: string | null = storage.getItem(combine(prefix, key));
      return value ? JSON.parse(value) : value;
    },
    remove: (key: string) => {
      storage.removeItem(combine(prefix, key));
    },
    clear: () => {
      storage.clear();
    },
  };

  encrypts.forEach((item) => {
    const enc = item();
    obj[`set${item.name}`] = (key: string, value: any) => {
      storage.setItem(combine(prefix, key), enc.encrypt(value));
    };
    obj[`get${item.name}`] = (key: string) => {
      const value: string | null = storage.getItem(combine(prefix, key));
      return value ? JSON.parse(value) : value;
    };
  });

  return obj;
}

const { VITE_STORAGE_PREFIX } = import.meta.env;
const { VITE_STORAGE } = import.meta.env;

const local = genStorage(localStorage, VITE_STORAGE_PREFIX);
const session = genStorage(sessionStorage, VITE_STORAGE_PREFIX);


export {

  local, session
}
